<script setup>
import api from "./api.js";
import EditDialog from "./edit.vue";
import RoleDialog from "./role.vue";

let organizationId = inject('organizationId');

const tableData = ref([])
const paging = reactive({
  total: 1,
  pageNum: 1,
  pageSize: 10,
})
const tableRef = ref()
const editDialog = ref();
const roleDialog = ref();

function handleAdd() {
  editDialog.value.open();
}

function handleUpdate(row) {
  editDialog.value.open(row);
}

function handleDelete(row) {
  api.del(row.userId).then(() => {
    getList();
  });
}

function handleRole(row) {
  roleDialog.value.open(row);
}

function getParams() {
  return {
    organizationId: organizationId.value,
  }
}

watch(organizationId, function () {
  getList();
})

function getList() {
  api.list(getParams(), paging).then(response => {
    tableData.value = response.records;
    paging.total = response.total;
    paging.pageNum = response.current;
  }).catch(error => {
    console.log(error);
  });
}

onMounted(()=> {
  getList();
})

</script>

<template>

  <div class="organization-structure-main" style="height: 100%;display: flex;flex-direction: column;">

    <div style="margin-bottom: 15px;">
      <el-button @click="handleAdd">增加</el-button>
    </div>
    <vxe-table style="height: 100%;flex-grow: 1;"
        border="full"
        ref="tableRef"
        :column-config="{resizable: true}"
        :data="tableData">
      <vxe-column field="userName" title="登录名"></vxe-column>
      <vxe-column field="nickName" title="真实姓名"></vxe-column>
      <vxe-column field="phonenumber" title="电话"></vxe-column>
      <vxe-column field="email" title="邮箱"></vxe-column>
      <vxe-column field="remark" title="备注"></vxe-column>
      <vxe-column width="250" title="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template #default="{ row }">
          <el-button size="small" link type="primary" @click="handleUpdate(row)">修改</el-button>
          <el-button size="small" link type="primary" @click="handleDelete(row)">删除</el-button>
          <el-button size="small" link type="primary" @click="handleRole(row)">角色</el-button>
        </template>
      </vxe-column>
    </vxe-table>

    <pagination
        v-show="paging.total>0"
        :total="paging.total"
        v-model:page="paging.pageNum"
        v-model:limit="paging.pageSize"
        @pagination="getList"
    />

    <EditDialog ref="editDialog" @sub="getList"></EditDialog>
    <RoleDialog ref="roleDialog" @sub="getList"></RoleDialog>

  </div>
</template>

<style scoped lang="scss">
  .organization-structure-main {
    box-sizing: border-box;
    padding: 15px;
  }
</style>